package io.ent.modules.product.controller;

import io.ent.common.utils.PageUtils;
import io.ent.common.utils.R;
import io.ent.common.validator.ValidatorUtils;
import io.ent.modules.product.entity.ProductCaseInfoEntity;
import io.ent.modules.product.entity.ProductCasePicsEntity;
import io.ent.modules.product.po.*;
import io.ent.modules.product.service.ProductCaseInfoService;
import io.ent.modules.product.service.ProductCasePicsService;
import io.ent.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-10-14 15:50:16
 */
@RestController
@RequestMapping("product/productcaseinfo")
public class ProductCaseInfoController {
    @Autowired
    private ProductCaseInfoService productCaseInfoService;
    @Autowired
    private ProductCasePicsService productCasePicsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:productcaseinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productCaseInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:productcaseinfo:info")
    public R info(@PathVariable("id") Integer id){
        Map<String, Object> map = new HashMap<>();
        ProductCaseInfoEntity productCaseInfo = productCaseInfoService.getById(id);
        List<ProductCasePicsEntity> pics = productCasePicsService.getAllPics(id,2);
        map.put("productCaseInfo", productCaseInfo);
        map.put("pics", pics);
        return R.ok().put("res", map);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:productcaseinfo:save")
    public R save(@RequestBody ProductCaseInfoEntityPo productCaseInfo){
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        productCaseInfo.setCreater(username);
        productCaseInfo.setCreateTime(new Date());
        productCaseInfo.setUpdater(username);
        productCaseInfo.setUpdateTime(new Date());
        productCaseInfoService.save(productCaseInfo);
        List<ProductPicPo> picList = productCaseInfo.getPicList();
        for (int i = 0; i < picList.size(); i++) {
            ProductCasePicsEntity picsEntity = new ProductCasePicsEntity();
            picsEntity.setPicId(productCaseInfo.getId());
            picsEntity.setPicName(picList.get(i).getPicName());
            picsEntity.setPicAddress(picList.get(i).getPicAddress());
            picsEntity.setPicType(2);
            picsEntity.setCreater(username);
            picsEntity.setCreateTime(new Date());
            picsEntity.setUpdater(username);
            picsEntity.setUpdateTime(new Date());
            productCasePicsService.save(picsEntity);
        }
        List<ProductPicPo> videoList = productCaseInfo.getVideoList();
        for (int i = 0; i < videoList.size(); i++) {
            ProductCasePicsEntity picsEntity = new ProductCasePicsEntity();
            picsEntity.setPicId(productCaseInfo.getId());
            picsEntity.setPicName(videoList.get(i).getPicName());
            picsEntity.setPicAddress(videoList.get(i).getPicAddress());
            picsEntity.setPicType(3);
            picsEntity.setCreater(username);
            picsEntity.setCreateTime(new Date());
            picsEntity.setUpdater(username);
            picsEntity.setUpdateTime(new Date());
            productCasePicsService.save(picsEntity);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:productcaseinfo:update")
    public R update(@RequestBody ProductCaseInfoEntityPo productCaseInfo){
        ValidatorUtils.validateEntity(productCaseInfo);
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        productCaseInfo.setUpdater(username);
        productCaseInfo.setUpdateTime(new Date());
        productCaseInfoService.updateById(productCaseInfo);
        List<ProductPicPo> picList = productCaseInfo.getPicList();
        for (int i = 0; i < picList.size(); i++) {
            ProductCasePicsEntity picsEntity = new ProductCasePicsEntity();
            picsEntity.setPicId(productCaseInfo.getId());
            picsEntity.setPicName(picList.get(i).getPicName());
            picsEntity.setPicAddress(picList.get(i).getPicAddress());
            picsEntity.setPicType(2);
            picsEntity.setCreater(username);
            picsEntity.setCreateTime(new Date());
            picsEntity.setUpdater(username);
            picsEntity.setUpdateTime(new Date());
            productCasePicsService.save(picsEntity);
        }
        List<ProductPicPo> videoList = productCaseInfo.getVideoList();
        for (int i = 0; i < videoList.size(); i++) {
            ProductCasePicsEntity picsEntity = new ProductCasePicsEntity();
            picsEntity.setPicId(productCaseInfo.getId());
            picsEntity.setPicName(videoList.get(i).getPicName());
            picsEntity.setPicAddress(videoList.get(i).getPicAddress());
            picsEntity.setPicType(3);
            picsEntity.setUpdater(username);
            picsEntity.setUpdateTime(new Date());
            productCasePicsService.save(picsEntity);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:productcaseinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        productCaseInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查找所有案例信息---已分页
     */
    @RequestMapping("/getCaseInfos")
    public R getCaseInfos(@RequestParam Map<String, Object> params){
        PageUtils productCaseInfos = productCaseInfoService.getProductCaseInfos(params);
        return R.ok().put("productCaseInfos",productCaseInfos);
    }

    /**
     * 查找所有案例信息---已分类
     */
    @RequestMapping("/getCaseTypeInfos")
    public R getCaseTypeInfos() {
        Map<String, Object> map = new HashMap<>();
        //查询客厅案例信息
        List<ProductCaseInfoEntityIfPo> kt = productCaseInfoService.getProductCaseInfosByTypeId(1);

        //查询餐厅案例信息
        List<ProductCaseInfoEntityIfPo> ct = productCaseInfoService.getProductCaseInfosByTypeId(2);

        //查询厨房案例信息
        List<ProductCaseInfoEntityIfPo> cf = productCaseInfoService.getProductCaseInfosByTypeId(3);

        //查询卫生间信息
        List<ProductCaseInfoEntityIfPo> wsj = productCaseInfoService.getProductCaseInfosByTypeId(4);

        //查询阳台案例信息
        List<ProductCaseInfoEntityIfPo> yt = productCaseInfoService.getProductCaseInfosByTypeId(5);

        map.put("kt", kt);
        map.put("ct", ct);
        map.put("cf", cf);
        map.put("wsj", wsj);
        map.put("yt", yt);
        return R.ok().put("caseInfos", map);
    }
}
